// 检查配置内容合法性功能
//

#include <iostream>
#include "str_util.h"
#include "check.h"


using namespace std;
using namespace su;


void Sheet::EachRow(std::function<void(const RowInfo& rowinfo)> fun) const
{
	for (auto& i : id2Row)
	{
		RowInfo rowinfo = {  &fieldName2Idx,  &i.second };
		LDEBUG("rowinfo row.size()=%ld", rowinfo.row->size());
		fun(rowinfo);
	}
}


const RowInfo* Sheet::FindRow(uint32 id) const
{
	auto it = id2Row.find(id);
	if (it == id2Row.end())
	{
		return nullptr;
	}
	RowInfo& row = id2RowInfo[id];
	row.fieldName2Idx = &fieldName2Idx;
	row.row = &it->second;
	return &row;
}

void CheckMgr::Check()
{
	LDEBUG("\n----------check start--------");
	bool bOk = true;
	for (auto& m : m_sheetname2CheckFun)
	{
		auto it = m_sheetname2sheet.find(m.first);
		if (it == m_sheetname2sheet.end())
		{
			LERROR("unknow check sheetname=%s", m.first.c_str());
			continue;
		}
		if (!m.second(it->second))
		{
			bOk = false;
		}
	}
	if (!bOk)
	{
		LERROR("----------check fail--------\n");
	}
	else
	{
		LDEBUG("----------check ok--------\n");
	}
}

Sheet& CheckMgr::GetSheetForWrite(const std::string& name)
{
	return m_sheetname2sheet[name];
}

const Sheet* CheckMgr::GetSheet(const std::string& name) const
{
	auto it = m_sheetname2sheet.find(name);
	if (it == m_sheetname2sheet.end())
	{
		return nullptr;
	}
	return &it->second;
}